---
title: "Supporting Veterans: Source Cues, Issue Ownership, and the Electoral Benefits of Military Service"
author: Peter T. McLaughlin, Sarina Rhinehart, & Matthew J. Geras
---

```{r Setup, include=FALSE, warning=FALSE, message=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(car)
library(psych)
library(tidyr)
library(dplyr)
library(janitor)
library(ggplot2)
library(stargazer)
library(cowplot)
library(estimatr) # Cluster SE
library(MASS) # ordered logit
library(stringr)
library(broom)
library(purrr)
library(flextable)
library(modelsummary)
library(mediation)
library(rio)


options(scipen=999)

# Set Local Working Directory
setwd()


## Load in data
dsorg <- import("Vets_Experiment_1_Data.RData") 

# Consent
table(dsorg$consent)
ds <- subset(dsorg, consent=="I agree to participate") 


```


```{r Treatment variable}

# Create Variable for treatment Group
table(ds$Dem_Vet_Experiment_DO, useNA="always") 
ds$treatment_group <- NA
ds$treatment_group[ds$Dem_Vet_Experiment_DO=="nonvet_crime|therm|issue_own|traits|cand_ideology|vote|manip|atten"]="nonvet_crime"
ds$treatment_group[ds$Dem_Vet_Experiment_DO=="nonvet_ns|therm|issue_own|traits|cand_ideology|vote|manip|atten"]= "nonvet_ns"
ds$treatment_group[ds$Dem_Vet_Experiment_DO=="vet_crime|therm|issue_own|traits|cand_ideology|vote|manip|atten"]="vet_crime"
ds$treatment_group[ds$Dem_Vet_Experiment_DO=="vet_ns|therm|issue_own|traits|cand_ideology|vote|manip|atten"]="vet_ns"

table(ds$Rep_Vet_Experiment_DO, useNA="always") 
ds$treatment_group[ds$Rep_Vet_Experiment_DO=="nonvet_crime|therm|issue_own|traits|cand_ideology|vote|manip|atten"]="nonvet_crime"
ds$treatment_group[ds$Rep_Vet_Experiment_DO=="nonvet_ns|therm|issue_own|traits|cand_ideology|vote|manip|atten"]= "nonvet_ns"
ds$treatment_group[ds$Rep_Vet_Experiment_DO=="vet_crime|therm|issue_own|traits|cand_ideology|vote|manip|atten"]="vet_crime"
ds$treatment_group[ds$Rep_Vet_Experiment_DO=="vet_ns|therm|issue_own|traits|cand_ideology|vote|manip|atten"]="vet_ns"


ds$treatment_group_f <- factor(ds$treatment_group)
ds$treatment_group_f = relevel(ds$treatment_group_f, ref="vet_ns")
```


```{r DVs}

# Feeling thermometer, issue ownership, and trait ownership
ds <- ds %>% 
mutate(therm = coalesce(therm_1, therm_1_d),
       defense_own = coalesce(issue_own_1, issue_own_1_d),
       patriotism = coalesce(traits_1, traits_1_d),
       courage = coalesce(traits_2, traits_2_d))

table(ds$therm, useNA="always")
table(ds$therm_1_d, useNA="always")
table(ds$therm_1, useNA="always")

table(ds$defense_own, useNA="always")
table(ds$issue_own_1, useNA="always")
table(ds$issue_own_1_d, useNA="always")

table(ds$patriotism, useNA="always")
table(ds$traits_1, useNA="always")
table(ds$traits_1_d, useNA="always")

table(ds$courage, useNA="always")
table(ds$traits_2, useNA="always")
table(ds$traits_2_d, useNA="always")


# Issue salience 
names(ds)[names(ds) == "issue_ranking_1"] <- "national_security"

table(ds$national_security, useNA="always")
```


```{r Demographic variables}
# Education
table(ds$education, useNA="always")
ds$education2 <- car::recode(ds$education, "'Less than high school diploma'=1;
                               'High school graduate or GED'=2; 'Some college'=3;'2-year degree'=4;
                               '4-year degree'=5; 'Post-graduate education'=6; else=NA")
# Gender
table(ds$woman, useNA="always")
ds$woman_f <- car::recode(ds$woman, "'Man'='Man';
                               'Woman'='Woman'; else=NA")
# Race
table(ds$race, useNA="always")
ds$race2 <- NA
ds$race2[ds$race=="White"]="White"
ds$race2[ds$race=="Asian American"]="Asian American"
ds$race2[ds$race=="Black or African American"]="Black"
ds$race2[ds$race=="Hispanic or Latino"]="Latino"
ds$race2[str_detect(ds$race, ",")]= "Mixed"
ds$race2[ds$race=="Native American or American Indian"]="Other"
ds$race2[ds$race=="Other"]="Other"
ds$race_f <- factor(ds$race2, ordered=FALSE)

# Ideology
table(ds$ideology, useNA="always")
ds$ideology2 <- car::recode(ds$ideology, "'Very Liberal'=1;
                           'Liberal'=2; 'Slightly Liberal'=3; 'Moderate'=4;
                            'Slightly Conservative'=5; 'Conservative'=6; 'Very Conservative'=7;
                           else=NA")
table(ds$ideology2, useNA="always")
ds$ideology_f <- as.integer(ds$ideology2)

# Income 
table(ds$income, useNA="always")
ds$income2 <- car::recode(ds$income, "'Under $10,000'=0; '$10,000-$19,999'=1;
                              '$20,000-$29,999'=2; '$30,000-$39,999'=3; '$40,000-$49,999'=4;
                              '$50,000-$59,999'=5; '$60,000-$74,999'=6; '$75,000-$89,999'=7;
                              '$90,000-$119,999'=8; '$120,000-$159,999'=9; '$160,000-$200,000'= 10;
                              'Over $200,000'=11;else=NA")
table(ds$income2, useNA="always")
ds$income_f <- as.integer(ds$income2)

# Religion
table(ds$religion, useNA="always")
ds$religion <- car::recode(ds$religion, "'Never'=1; 'A few times a year'=2; 'Once or twice a month'=3;
                              'Almost every week'=4; 'Every week'=5; else=NA")
table(ds$religion, useNA="always")
ds$religion_f <- as.integer(ds$religion)

# Age
table(ds$age)
ds$age_f <- ds$age

# Region
table(ds$region) # 1= Northeast; 2= Midwest; 3= South; 4= West
ds$region_f <- ds$region

# Party
table(ds$party, useNA="always")
ds$party_f <- car::recode(ds$party, "'Independent'='Independent'; 'Not very strong Democrat'='Democrat';
                              'Not very strong Republican'='Republican'; 'Strong Democrat'='Democrat';
                             'Strong Republican'='Republican'; else=NA")
ds$party2_f <- NA
ds$party2_f[ds$party_f=="Democrat"]="Democrat"
ds$party2_f[ds$party_f=="Republican"]="Republican"
ds$party2_f[ds$independent=="More similar to Democrats than Republicans"]="Democrat"
ds$party2_f[ds$independent=="More similar to Republicans than Democrats"]="Republican"
ds$party2_f = factor(ds$party2_f)
ds$party2_f = relevel(ds$party2_f, ref="Democrat")

# Candidate party
table(ds$FL_66_DO, useNA="always")
ds$candidate_party <- NA

ds$candidate_party[ds$FL_66_DO=="Rep_Vet_Experiment"]="rep"
ds$candidate_party[ds$FL_66_DO=="Dem_Vet_Experiment"]="dem"

table(ds$candidate_party, useNA="always") 


# Partisan Match variable
table(ds$candidate_party)
table(ds$party_f)

mutate(ds, partisan_match = ifelse(party_f == 'Independent', 0,
                                     ifelse(party_f == 'Democrat' & candidate_party == 'dem', 1,
                                            ifelse(party_f == 'Republican' & candidate_party == 'rep', 1, -1)))) -> ds

```



```{r Attention Check Filter}

table(ds$atten_d, useNA="always") 

ds$atten_d_r <- as.numeric(car::recode(ds$atten_d, "'Kansas'= 1; 'Florida'= 0; 'Maine'= 0; 'Oregon'= 0;else=NA"))
table(ds$atten_d_r, useNA="always")

table(ds$atten, useNA="always")

ds$atten_r <- as.numeric(car::recode(ds$atten, "'Kansas'= 1; 'Florida'= 0; 'Maine'= 0; 'Oregon'= 0;else=NA"))
table(ds$atten_r, useNA="always")

# Combine
ds <- ds %>% 
mutate(atten_combined = coalesce(atten_d_r, atten_r))

table(ds$atten_combined, useNA="always") 

# Filer data for attention
ds.att <- ds %>%
  filter(ds$atten_combined == 1)
```


```{r Sample demographics}

tabyl(ds.att$education2, sort=T) 

tabyl(ds.att$woman_f, sort=T) 

tabyl(ds.att$race_f, sort=T)   

describe(ds.att$income_f) 

tabyl(ds.att$party_f, sort=T) 

tabyl(ds.att$ideology_f, sort=T) 

tabyl(ds.att$religion_f, sort=T) 

describe(ds.att$age_f) 

tabyl(ds.att$region_f) 
```


```{r Feeling Thermometer Fig}
# Veteran National Security
ds_vet_ns <- subset(ds.att, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$therm)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran Crime
ds_vet_crime <- subset(ds.att, treatment_group_f=="vet_crime")
test_vet_crime <- t.test(ds_vet_crime$therm)
vet_crime_mean <- test_vet_crime$estimate
vet_crime_lower <- test_vet_crime$conf.int[1]
vet_crime_upper <- test_vet_crime$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds.att, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$therm)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]


# Nonveteran Crime
ds_nonvet_crime <- subset(ds.att, treatment_group_f=="nonvet_crime")
test_nonvet_crime <- t.test(ds_nonvet_crime$therm)
nonvet_crime_mean <- test_nonvet_crime$estimate
nonvet_crime_lower <- test_nonvet_crime$conf.int[1]
nonvet_crime_upper <- test_nonvet_crime$conf.int[2]


means <- c(vet_ns_mean, vet_crime_mean,
           nonvet_ns_mean, nonvet_crime_mean)
lower <- c(vet_ns_lower, vet_crime_lower,
           nonvet_ns_lower, nonvet_crime_lower)
upper <- c(vet_ns_upper, vet_crime_upper,
           nonvet_ns_upper, nonvet_crime_upper)
vet_group <- c("Veteran","Veteran", "Nonveteran","Nonveteran")
issue_group <- c("National Defense","Crime","National Defense","Crime")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_y_continuous(limits=c(55, 75))+
  scale_x_discrete(guide = guide_axis(angle = 20)) +
   labs(title = "All Respondents", x ="", y = "Mean Feeling Thermometer Rating \n")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> therm_fig
```


```{r Defense Owbership Fig}

# Veteran National Security
ds_vet_ns <- subset(ds.att, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$defense_own)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran Crime
ds_vet_crime <- subset(ds.att, treatment_group_f=="vet_crime")
test_vet_crime <- t.test(ds_vet_crime$defense_own)
vet_crime_mean <- test_vet_crime$estimate
vet_crime_lower <- test_vet_crime$conf.int[1]
vet_crime_upper <- test_vet_crime$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds.att, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$defense_own)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]


# Nonveteran Crime
ds_nonvet_crime <- subset(ds.att, treatment_group_f=="nonvet_crime")
test_nonvet_crime <- t.test(ds_nonvet_crime$defense_own)
nonvet_crime_mean <- test_nonvet_crime$estimate
nonvet_crime_lower <- test_nonvet_crime$conf.int[1]
nonvet_crime_upper <- test_nonvet_crime$conf.int[2]


means <- c(vet_ns_mean, vet_crime_mean,
           nonvet_ns_mean, nonvet_crime_mean)
lower <- c(vet_ns_lower, vet_crime_lower,
           nonvet_ns_lower, nonvet_crime_lower)
upper <- c(vet_ns_upper, vet_crime_upper,
           nonvet_ns_upper, nonvet_crime_upper)
vet_group <- c("Veteran","Veteran", "Nonveteran","Nonveteran")
issue_group <- c("National Defense","Crime","National Defense","Crime")
defense_own_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
defense_own_condensed_d <- as.data.frame(defense_own_condensed_d)
defense_own_condensed_d$means <- as.numeric(as.character(defense_own_condensed_d$means))
defense_own_condensed_d$lower <- as.numeric(as.character(defense_own_condensed_d$lower))
defense_own_condensed_d$upper <- as.numeric(as.character(defense_own_condensed_d$upper))

ggplot(defense_own_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
    scale_y_continuous(limits=c(5, 8))+
  scale_x_discrete(guide = guide_axis(angle = 20)) +
 labs(title = "National Defense \n Issue Ownership", x ="", y = "Mean Rating \n")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> defense_own_fig
```

```{r Patriotism Fig}

# Veteran National Security
ds_vet_ns <- subset(ds.att, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$patriotism)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran Crime
ds_vet_crime <- subset(ds.att, treatment_group_f=="vet_crime")
test_vet_crime <- t.test(ds_vet_crime$patriotism)
vet_crime_mean <- test_vet_crime$estimate
vet_crime_lower <- test_vet_crime$conf.int[1]
vet_crime_upper <- test_vet_crime$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds.att, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$patriotism)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]


# Nonveteran Crime
ds_nonvet_crime <- subset(ds.att, treatment_group_f=="nonvet_crime")
test_nonvet_crime <- t.test(ds_nonvet_crime$patriotism)
nonvet_crime_mean <- test_nonvet_crime$estimate
nonvet_crime_lower <- test_nonvet_crime$conf.int[1]
nonvet_crime_upper <- test_nonvet_crime$conf.int[2]


means <- c(vet_ns_mean, vet_crime_mean,
           nonvet_ns_mean, nonvet_crime_mean)
lower <- c(vet_ns_lower, vet_crime_lower,
           nonvet_ns_lower, nonvet_crime_lower)
upper <- c(vet_ns_upper, vet_crime_upper,
           nonvet_ns_upper, nonvet_crime_upper)
vet_group <- c("Veteran","Veteran", "Nonveteran","Nonveteran")
issue_group <- c("National Defense","Crime","National Defense","Crime")
patriotism_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
patriotism_condensed_d <- as.data.frame(patriotism_condensed_d)
patriotism_condensed_d$means <- as.numeric(as.character(patriotism_condensed_d$means))
patriotism_condensed_d$lower <- as.numeric(as.character(patriotism_condensed_d$lower))
patriotism_condensed_d$upper <- as.numeric(as.character(patriotism_condensed_d$upper))

ggplot(patriotism_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
      scale_y_continuous(limits=c(5, 8))+
  scale_x_discrete(guide = guide_axis(angle = 20)) +
  labs(title = "Patriotism \n Trait Ownership", x ="Issue Discussed", y = "")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> patriotism_fig
```


```{r Courage Fig}

# Veteran National Security
ds_vet_ns <- subset(ds.att, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$courage)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran Crime
ds_vet_crime <- subset(ds.att, treatment_group_f=="vet_crime")
test_vet_crime <- t.test(ds_vet_crime$courage)
vet_crime_mean <- test_vet_crime$estimate
vet_crime_lower <- test_vet_crime$conf.int[1]
vet_crime_upper <- test_vet_crime$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds.att, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$courage)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]


# Nonveteran Crime
ds_nonvet_crime <- subset(ds.att, treatment_group_f=="nonvet_crime")
test_nonvet_crime <- t.test(ds_nonvet_crime$courage)
nonvet_crime_mean <- test_nonvet_crime$estimate
nonvet_crime_lower <- test_nonvet_crime$conf.int[1]
nonvet_crime_upper <- test_nonvet_crime$conf.int[2]


means <- c(vet_ns_mean, vet_crime_mean,
           nonvet_ns_mean, nonvet_crime_mean)
lower <- c(vet_ns_lower, vet_crime_lower,
           nonvet_ns_lower, nonvet_crime_lower)
upper <- c(vet_ns_upper, vet_crime_upper,
           nonvet_ns_upper, nonvet_crime_upper)
vet_group <- c("Veteran","Veteran", "Nonveteran","Nonveteran")
issue_group <- c("National Defense","Crime","National Defense","Crime")
courage_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
courage_condensed_d <- as.data.frame(courage_condensed_d)
courage_condensed_d$means <- as.numeric(as.character(courage_condensed_d$means))
courage_condensed_d$lower <- as.numeric(as.character(courage_condensed_d$lower))
courage_condensed_d$upper <- as.numeric(as.character(courage_condensed_d$upper))

ggplot(courage_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
        scale_y_continuous(limits=c(5, 8))+
  scale_x_discrete(guide = guide_axis(angle = 20)) +
  labs(title = "Courage \n Trait Ownership", x ="", y = "")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif")))  -> courage_fig
```

```{r Figure_4}
# Combine Trait Figs
plot_grid(defense_own_fig, patriotism_fig, courage_fig, ncol = 3) # 4 x 8.75 PDF
```

```{r National Security Salience Fig}

# Veteran National Security
ds_vet_ns <- subset(ds.att, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$national_security)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran Crime
ds_vet_crime <- subset(ds.att, treatment_group_f=="vet_crime")
test_vet_crime <- t.test(ds_vet_crime$national_security)
vet_crime_mean <- test_vet_crime$estimate
vet_crime_lower <- test_vet_crime$conf.int[1]
vet_crime_upper <- test_vet_crime$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds.att, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$national_security)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]


# Nonveteran Crime
ds_nonvet_crime <- subset(ds.att, treatment_group_f=="nonvet_crime")
test_nonvet_crime <- t.test(ds_nonvet_crime$national_security)
nonvet_crime_mean <- test_nonvet_crime$estimate
nonvet_crime_lower <- test_nonvet_crime$conf.int[1]
nonvet_crime_upper <- test_nonvet_crime$conf.int[2]


means <- c(vet_ns_mean, vet_crime_mean,
           nonvet_ns_mean, nonvet_crime_mean)
lower <- c(vet_ns_lower, vet_crime_lower,
           nonvet_ns_lower, nonvet_crime_lower)
upper <- c(vet_ns_upper, vet_crime_upper,
           nonvet_ns_upper, nonvet_crime_upper)
vet_group <- c("Veteran","Veteran", "Nonveteran","Nonveteran")
issue_group <- c("National Defense","Crime","National Defense","Crime")
national_security_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
national_security_condensed_d <- as.data.frame(national_security_condensed_d)
national_security_condensed_d$means <- as.numeric(as.character(national_security_condensed_d$means))
national_security_condensed_d$lower <- as.numeric(as.character(national_security_condensed_d$lower))
national_security_condensed_d$upper <- as.numeric(as.character(national_security_condensed_d$upper))

ggplot(national_security_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
  labs(x ="Issue Discussed", y = "Mean Defense Salience Rating \n")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> figure_5 # 4 x 4 PDF
```


```{r T-tests}

ds_vets <- subset(ds.att, treatment_group_f %in% c("vet_ns","vet_crime"))

t.test(ds_vets$therm ~ ds_vets$treatment_group_f) -> vets_therm
t.test(ds_vets$defense_own ~ ds_vets$treatment_group_f) -> vets_issue_own
t.test(ds_vets$patriotism ~ ds_vets$treatment_group_f) -> vets_patriotism
t.test(ds_vets$courage ~ ds_vets$treatment_group_f) -> vets_courage
t.test(ds_vets$national_security ~ ds_vets$treatment_group_f) -> vets_salience

ds_nat_sec <- subset(ds.att, treatment_group_f %in% c("vet_ns","nonvet_ns"))

t.test(ds_nat_sec$therm ~ ds_nat_sec$treatment_group_f) -> ns_therm
t.test(ds_nat_sec$defense_own ~ ds_nat_sec$treatment_group_f) -> ns_issue_own
t.test(ds_nat_sec$patriotism ~ ds_nat_sec$treatment_group_f) -> ns_patriotism
t.test(ds_nat_sec$courage ~ ds_nat_sec$treatment_group_f) -> ns_courage
t.test(ds_nat_sec$national_security ~ ds_nat_sec$treatment_group_f) -> ns_salience

ds_nonvets <- subset(ds.att, treatment_group_f %in% c("nonvet_ns","nonvet_crime"))

t.test(ds_nonvets$therm ~ ds_nonvets$treatment_group_f) -> nonvets_therm
t.test(ds_nonvets$defense_own ~ ds_nonvets$treatment_group_f) -> nonvets_issue_own
t.test(ds_nonvets$patriotism ~ ds_nonvets$treatment_group_f) -> nonvets_patriotism
t.test(ds_nonvets$courage ~ ds_nonvets$treatment_group_f) -> nonvets_courage
t.test(ds_nonvets$national_security ~ ds_nonvets$treatment_group_f) -> nonvets_salience

ds_crime <- subset(ds.att, treatment_group_f %in% c("vet_crime","nonvet_crime"))

t.test(ds_crime$therm ~ ds_crime$treatment_group_f) -> crime_therm
t.test(ds_crime$defense_own ~ ds_crime$treatment_group_f) -> crime_issue_own
t.test(ds_crime$patriotism ~ ds_crime$treatment_group_f) -> crime_patriotism
t.test(ds_crime$courage ~ ds_crime$treatment_group_f) -> crime_courage
t.test(ds_crime$national_security ~ ds_crime$treatment_group_f) -> crime_salience

## Tables
# Set Function
pvalue_format <- function(x){
  z <- cut(as.numeric(x), breaks = c(-Inf, 0.01, 0.05, 0.1, Inf), labels = c("***", "**", "*", ""))
  as.character(z)
}

# Vets only 
tab_vets <- map_df(list(vets_therm, vets_issue_own, vets_patriotism, vets_courage, vets_salience), .id = "dv", tidy)  
table_vets <- tab_vets[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_vets,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "National Defense", estimate2 = "Crime",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA1
  
# Nonvets only 
tab_nonvets <- map_df(list(nonvets_therm, nonvets_issue_own, nonvets_patriotism, nonvets_courage, nonvets_salience), .id = "dv", tidy)  
table_nonvets <- tab_nonvets[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_nonvets,
 col_keys = c("dv", "estimate", "estimate2", "estimate1", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Crime", estimate2 = "National Defense",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA2 # NOTE: We manually changed estimate signs to match the TableA1 


# National Defence Only 
tab_ns <- map_df(list(ns_therm, ns_issue_own, ns_patriotism, ns_courage, ns_salience), .id = "dv", tidy)  
table_ns <- tab_ns[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_ns,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Veteran", estimate2 = "Nonveteran",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA3
  
  
# Crime only 
tab_crime <- map_df(list(crime_therm, crime_issue_own, crime_patriotism, crime_courage, crime_salience), .id = "dv", tidy)  
table_crime <- tab_crime[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_crime,
    col_keys = c("dv", "estimate", "estimate2", "estimate1", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Nonveteran", estimate2 = "Veteran",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA4 # NOTE: We manually changed estimate signs to match the TableA3 
  
```


```{r Republicans Only Therm Fig}

ds.atten_r <- ds %>%
  filter(ds$atten_combined == 1 & ds$party2_f == "Republican")

# Veteran National Security
ds_vet_ns <- subset(ds.atten_r, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$therm)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran Crime
ds_vet_crime <- subset(ds.atten_r, treatment_group_f=="vet_crime")
test_vet_crime <- t.test(ds_vet_crime$therm)
vet_crime_mean <- test_vet_crime$estimate
vet_crime_lower <- test_vet_crime$conf.int[1]
vet_crime_upper <- test_vet_crime$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds.atten_r, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$therm)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]


# Nonveteran Crime
ds_nonvet_crime <- subset(ds.atten_r, treatment_group_f=="nonvet_crime")
test_nonvet_crime <- t.test(ds_nonvet_crime$therm)
nonvet_crime_mean <- test_nonvet_crime$estimate
nonvet_crime_lower <- test_nonvet_crime$conf.int[1]
nonvet_crime_upper <- test_nonvet_crime$conf.int[2]


means <- c(vet_ns_mean, vet_crime_mean,
           nonvet_ns_mean, nonvet_crime_mean)
lower <- c(vet_ns_lower, vet_crime_lower,
           nonvet_ns_lower, nonvet_crime_lower)
upper <- c(vet_ns_upper, vet_crime_upper,
           nonvet_ns_upper, nonvet_crime_upper)
vet_group <- c("Veteran","Veteran", "Nonveteran","Nonveteran")
issue_group <- c("National Defense","Crime","National Defense","Crime")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_y_continuous(limits=c(55, 75))+
  scale_x_discrete(guide = guide_axis(angle = 20)) +
   labs(title = "Republicans", x ="Issue Discussed", y = "")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> therm_fig_r
```

```{r Republicans Only T-tests}

ds_vets_r <- subset(ds.atten_r, treatment_group_f %in% c("vet_ns","vet_crime"))

t.test(ds_vets_r$therm ~ ds_vets_r$treatment_group_f) -> vets_therm_r
t.test(ds_vets_r$defense_own ~ ds_vets_r$treatment_group_f) -> vets_issue_own_r
t.test(ds_vets_r$patriotism ~ ds_vets_r$treatment_group_f) -> vets_patriotism_r
t.test(ds_vets_r$courage ~ ds_vets_r$treatment_group_f) -> vets_courage_r
t.test(ds_vets_r$national_security ~ ds_vets_r$treatment_group_f) -> vets_salience_r

ds_nat_sec_r <- subset(ds.atten_r, treatment_group_f %in% c("vet_ns","nonvet_ns"))

t.test(ds_nat_sec_r$therm ~ ds_nat_sec_r$treatment_group_f) -> ns_therm_r
t.test(ds_nat_sec_r$defense_own ~ ds_nat_sec_r$treatment_group_f) -> ns_issue_own_r
t.test(ds_nat_sec_r$patriotism ~ ds_nat_sec_r$treatment_group_f) -> ns_patriotism_r
t.test(ds_nat_sec_r$courage ~ ds_nat_sec_r$treatment_group_f) -> ns_courage_r
t.test(ds_nat_sec_r$national_security ~ ds_nat_sec_r$treatment_group_f) -> ns_salience_r

ds_nonvets_r <- subset(ds.atten_r, treatment_group_f %in% c("nonvet_ns","nonvet_crime"))

t.test(ds_nonvets_r$therm ~ ds_nonvets_r$treatment_group_f) -> nonvets_therm_r
t.test(ds_nonvets_r$defense_own ~ ds_nonvets_r$treatment_group_f) -> nonvets_issue_own_r
t.test(ds_nonvets_r$patriotism ~ ds_nonvets_r$treatment_group_f) -> nonvets_patriotism_r
t.test(ds_nonvets_r$courage ~ ds_nonvets_r$treatment_group_f) -> nonvets_courage_r
t.test(ds_nonvets_r$national_security ~ ds_nonvets_r$treatment_group_f) -> nonvets_salience_r

ds_crime_r <- subset(ds.atten_r, treatment_group_f %in% c("vet_crime","nonvet_crime"))

t.test(ds_crime_r$therm ~ ds_crime_r$treatment_group_f) -> crime_therm_r
t.test(ds_crime_r$defense_own ~ ds_crime_r$treatment_group_f) -> crime_issue_own_r
t.test(ds_crime_r$patriotism ~ ds_crime_r$treatment_group_f) -> crime_patriotism_r
t.test(ds_crime_r$courage ~ ds_crime_r$treatment_group_f) -> crime_courage_r
t.test(ds_crime_r$national_security ~ ds_crime_r$treatment_group_f) -> crime_salience_r

## Tables
# Set Function
pvalue_format <- function(x){
  z <- cut(as.numeric(x), breaks = c(-Inf, 0.01, 0.05, 0.1, Inf), labels = c("***", "**", "*", ""))
  as.character(z)
}

# Vets only 
tab_vets_r <- map_df(list(vets_therm_r, vets_issue_own_r, vets_patriotism_r, vets_courage_r, vets_salience_r), .id = "dv", tidy)  
table_vets_r <- tab_vets_r[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_vets_r,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "National Defense", estimate2 = "Crime",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA5
  
# Nonvets only 
tab_nonvets_r <- map_df(list(nonvets_therm_r, nonvets_issue_own_r, 
                             nonvets_patriotism_r, nonvets_courage_r, nonvets_salience_r), .id = "dv", tidy)  
table_nonvets_r <- tab_nonvets_r[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_nonvets_r,
    col_keys = c("dv", "estimate", "estimate2", "estimate1", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Crime", estimate2 = "National Defense",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit()  -> TableA6
  
# NOTE: We manually changed estimate signs to match the TableA5 

# National Defense Only 
tab_ns_r <- map_df(list(ns_therm_r, ns_issue_own_r, ns_patriotism_r, ns_courage_r, ns_salience_r), .id = "dv", tidy)  
table_ns_r <- tab_ns_r[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_ns_r,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Veteran", estimate2 = "Nonveteran",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA7
  

  
# Crime only 
tab_crime_r <- map_df(list(crime_therm_r, crime_issue_own_r, crime_patriotism_r, crime_courage_r, crime_salience_r), .id = "dv", tidy)  
table_crime_r <- tab_crime_r[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_crime_r,
    col_keys = c("dv", "estimate", "estimate2", "estimate1", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Nonveteran", estimate2 = "Veteran",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA8

# NOTE: We manually changed estimate signs to match the TableA7 

```


```{r Democrats Only Therm Fig}

ds.atten_d <- ds %>%
  filter(ds$atten_combined == 1 & ds$party2_f == "Democrat")

# Veteran National Security
ds_vet_ns <- subset(ds.atten_d, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$therm)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran Crime
ds_vet_crime <- subset(ds.atten_d, treatment_group_f=="vet_crime")
test_vet_crime <- t.test(ds_vet_crime$therm)
vet_crime_mean <- test_vet_crime$estimate
vet_crime_lower <- test_vet_crime$conf.int[1]
vet_crime_upper <- test_vet_crime$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds.atten_d, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$therm)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]


# Nonveteran Crime
ds_nonvet_crime <- subset(ds.atten_d, treatment_group_f=="nonvet_crime")
test_nonvet_crime <- t.test(ds_nonvet_crime$therm)
nonvet_crime_mean <- test_nonvet_crime$estimate
nonvet_crime_lower <- test_nonvet_crime$conf.int[1]
nonvet_crime_upper <- test_nonvet_crime$conf.int[2]


means <- c(vet_ns_mean, vet_crime_mean,
           nonvet_ns_mean, nonvet_crime_mean)
lower <- c(vet_ns_lower, vet_crime_lower,
           nonvet_ns_lower, nonvet_crime_lower)
upper <- c(vet_ns_upper, vet_crime_upper,
           nonvet_ns_upper, nonvet_crime_upper)
vet_group <- c("Veteran","Veteran", "Nonveteran","Nonveteran")
issue_group <- c("National Defense","Crime","National Defense","Crime")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_y_continuous(limits=c(55, 75))+
  scale_x_discrete(guide = guide_axis(angle = 20)) +
   labs(title = "Democrats", x ="", y = "")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> therm_fig_d
```


```{r Democrats Only T-tests}

ds_vets_d <- subset(ds.atten_d, treatment_group_f %in% c("vet_ns","vet_crime"))

t.test(ds_vets_d$therm ~ ds_vets_d$treatment_group_f) -> vets_therm_d
t.test(ds_vets_d$defense_own ~ ds_vets_d$treatment_group_f) -> vets_issue_own_d
t.test(ds_vets_d$patriotism ~ ds_vets_d$treatment_group_f) -> vets_patriotism_d
t.test(ds_vets_d$courage ~ ds_vets_d$treatment_group_f) -> vets_courage_d
t.test(ds_vets_d$national_security ~ ds_vets_d$treatment_group_f) -> vets_salience_d

ds_nat_sec_d <- subset(ds.atten_d, treatment_group_f %in% c("vet_ns","nonvet_ns"))

t.test(ds_nat_sec_d$therm ~ ds_nat_sec_d$treatment_group_f) -> ns_therm_d
t.test(ds_nat_sec_d$defense_own ~ ds_nat_sec_d$treatment_group_f) -> ns_issue_own_d
t.test(ds_nat_sec_d$patriotism ~ ds_nat_sec_d$treatment_group_f) -> ns_patriotism_d
t.test(ds_nat_sec_d$courage ~ ds_nat_sec_d$treatment_group_f) -> ns_courage_d
t.test(ds_nat_sec_d$national_security ~ ds_nat_sec_d$treatment_group_f) -> ns_salience_d

ds_nonvets_d <- subset(ds.atten_d, treatment_group_f %in% c("nonvet_ns","nonvet_crime"))

t.test(ds_nonvets_d$therm ~ ds_nonvets_d$treatment_group_f) -> nonvets_therm_d
t.test(ds_nonvets_d$defense_own ~ ds_nonvets_d$treatment_group_f) -> nonvets_issue_own_d
t.test(ds_nonvets_d$patriotism ~ ds_nonvets_d$treatment_group_f) -> nonvets_patriotism_d
t.test(ds_nonvets_d$courage ~ ds_nonvets_d$treatment_group_f) -> nonvets_courage_d
t.test(ds_nonvets_d$national_security ~ ds_nonvets_d$treatment_group_f) -> nonvets_salience_d

ds_crime_d <- subset(ds.atten_d, treatment_group_f %in% c("vet_crime","nonvet_crime"))

t.test(ds_crime_d$therm ~ ds_crime_d$treatment_group_f) -> crime_therm_d
t.test(ds_crime_d$defense_own ~ ds_crime_d$treatment_group_f) -> crime_issue_own_d
t.test(ds_crime_d$patriotism ~ ds_crime_d$treatment_group_f) -> crime_patriotism_d
t.test(ds_crime_d$courage ~ ds_crime_d$treatment_group_f) -> crime_courage_d
t.test(ds_crime_d$national_security ~ ds_crime_d$treatment_group_f) -> crime_salience_d

## Tables
# Set Function
pvalue_format <- function(x){
  z <- cut(as.numeric(x), breaks = c(-Inf, 0.01, 0.05, 0.1, Inf), labels = c("***", "**", "*", ""))
  as.character(z)
}

# Vets only 
tab_vets_d <- map_df(list(vets_therm_d, vets_issue_own_d, vets_patriotism_d, vets_courage_d, vets_salience_d), .id = "dv", tidy)  
table_vets_d <- tab_vets_d[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_vets_d,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "National Defense", estimate2 = "Crime",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA9
  
# Nonvets only 
tab_nonvets_d <- map_df(list(nonvets_therm_d, nonvets_issue_own_d, nonvets_patriotism_d, nonvets_courage_d, nonvets_salience_d), .id = "dv", tidy)  
table_nonvets_d <- tab_nonvets_d[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_nonvets_d,
    col_keys = c("dv", "estimate", "estimate2", "estimate1", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Crime", estimate2 = "National Defense",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA10

# NOTE: We manually changed estimate signs to match the TableA9 

# National Security only 
tab_ns_d <- map_df(list(ns_therm_d, ns_issue_own_d, ns_patriotism_d, ns_courage_d, ns_salience_d), .id = "dv", tidy)  
table_ns_d <- tab_ns_d[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_ns_d,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Veteran", estimate2 = "Nonveteran",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA11

  
# Crime only 
tab_crime_d <- map_df(list(crime_therm_d, crime_issue_own_d, crime_patriotism_d, crime_courage_d, crime_salience_d), .id = "dv", tidy)  
table_crime_d <- tab_crime_d[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_crime_d,
    col_keys = c("dv", "estimate", "estimate2", "estimate1", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Nonveteran", estimate2 = "Veteran",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA12

# NOTE: We manually changed estimate signs to match the TableA11 

```

```{r Figure 3}
# Combine Party Figs
plot_grid(therm_fig, therm_fig_r, therm_fig_d, ncol = 3) # 4 x 8.75 PDF
```


```{r T-test with tukey comparisions}

#remove nonvet crime
ds_anova <- subset(ds.att, treatment_group_f!="nonvet_crime")

#therm
therm_anova <- aov(therm~treatment_group_f, data=ds_anova)
summary(therm_anova)
TukeyHSD(therm_anova, conf.level=.95) -> tukey_therm

#defense own
defense_own_anova <- aov(defense_own~treatment_group_f, data=ds_anova)
summary(defense_own_anova)
TukeyHSD(defense_own_anova, conf.level=.95) -> tukey_def_own

#patriotism
patriotism_anova <- aov(patriotism~treatment_group_f, data=ds_anova)
summary(patriotism_anova)
TukeyHSD(patriotism_anova, conf.level=.95) -> tukey_patriotism

#courage
courage_anova <- aov(courage~treatment_group_f, data=ds_anova)
summary(courage_anova)
TukeyHSD(courage_anova, conf.level=.95) -> tukey_courage

#salience
salience_anova <- aov(national_security~treatment_group_f, data=ds_anova)
summary(salience_anova)
TukeyHSD(salience_anova, conf.level=.95) -> tukey_salience

# Tables
tukey_df <- map_df(list(tukey_therm, tukey_def_own, tukey_patriotism, tukey_courage, tukey_salience), .id = "dv", tidy)
table_tukey <- tukey_df[c("dv", "contrast", "estimate", "adj.p.value")]

  flextable(table_tukey,
    col_keys = c("dv", "contrast", "estimate", "adj.p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(adj.p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", 
                    contrast = "Contrast", estimate = "Diff. of Means",
                    adj.p.value = "Adjusted Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "adj.p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableA13
```

```{r  Models With Party Match}

# Models
mod_therm_pm <- lm(therm ~ treatment_group_f  +  partisan_match, ds.att)
summary(mod_therm_pm)

mod_defense_own_pm <- lm(defense_own ~ treatment_group_f + partisan_match, ds.att)
summary(mod_defense_own_pm)

mod_patriotism_pm <- lm(patriotism ~ treatment_group_f + partisan_match, ds.att)
summary(mod_patriotism_pm)

mod_courage_pm <- lm(courage ~ treatment_group_f + partisan_match, ds.att)
summary(mod_courage_pm)

mod_salience_pm <- lm(national_security ~ treatment_group_f + partisan_match, ds.att)
summary(mod_salience_pm)

# Table
models_full <- list()
models_full[['Feeling Thermometer']] <- mod_therm_pm
models_full[['Defense Issue Ownership']] <- mod_defense_own_pm
models_full[['Patriotism']] <- mod_patriotism_pm
models_full[['Courage']] <- mod_courage_pm
models_full[['Salience']] <- mod_salience_pm

coefs <- c("treatment_group_fnonvet_ns" = "Treatment: Nonvet. / Nat. Def.",
          "treatment_group_fvet_crime" = "Treatment: Vet. / Crime", 
          "treatment_group_fnonvet_crime" = "Treatment: Nonvet. / Crime",
          "partisan_match" = "Partisan Match",
          "(Intercept)" = "Constant")

modelsummary(models_full, 
             stars = c('*' = .1, '**' = .05, '***' = .01),
             coef_map = coefs,
             title = '', #output = "Table_Full.docx"
             ) -> TableA14

```


```{r  Models With Party Interaction}

# Models
mod_therm_i <- lm(therm ~ treatment_group_f * party2_f  +  partisan_match, ds.att)
summary(mod_therm_i)

mod_defense_own_i <- lm(defense_own ~ treatment_group_f * party2_f  + partisan_match, ds.att)
summary(mod_defense_own_i)

mod_patriotism_i <- lm(patriotism ~ treatment_group_f * party2_f  + partisan_match, ds.att)
summary(mod_patriotism_i)

mod_courage_i <- lm(courage ~ treatment_group_f * party2_f + partisan_match, ds.att)
summary(mod_courage_i)

mod_salience_i <- lm(national_security ~ treatment_group_f * party2_f + partisan_match, ds.att)
summary(mod_salience_i)

# Table
models_int <- list()
models_int[['Feeling Thermometer']] <- mod_therm_i
models_int[['Defense Issue Ownership']] <- mod_defense_own_i
models_int[['Patriotism']] <- mod_patriotism_i
models_int[['Courage']] <- mod_courage_i
models_int[['Salience']] <- mod_salience_i

coefs <- c("treatment_group_fnonvet_ns" = "Treatment: Nonvet. / Nat. Def.",
          "treatment_group_fvet_crime" = "Treatment: Vet. / Crime", 
          "treatment_group_fnonvet_crime" = "Treatment: Nonvet. / Crime",
          "party2_fRepublican" = "Respondent Party: Republican",
          "partisan_match" = "Partisan Match",
          "treatment_group_fnonvet_ns:party2_fRepublican" = "Nonvet. / Nat. Def. X Rep. Respondent",
          "treatment_group_fvet_crime:party2_fRepublican" = "Vet. / Crime X Rep. Respondent",
          "treatment_group_fnonvet_crime:party2_fRepublican" = "Nonvet. / Crime X Rep. Respondent",
          "(Intercept)" = "Constant")

modelsummary(models_int, 
             stars = c('*' = .1, '**' = .05, '***' = .01),
             coef_map = coefs,
             title = '', #output = "Table_int_exp2.docx"
             ) -> TableA15

```
